package leetcode.editor.cn.q1xx

import leetcode.editor.cn.TreeNode

class Q108 {
    fun sortedArrayToBST(nums: IntArray): TreeNode? {
        return buildBST(nums, 0, nums.size - 1)
    }

    fun buildBST(nums: IntArray, left: Int, right: Int): TreeNode? {
        if (left > right)
            return null

        val mid = (left + right) / 2
        val root = TreeNode(nums[mid])
        root.left = buildBST(nums, left, mid - 1)
        root.right = buildBST(nums, mid + 1, right)
        return root
    }
}